package com.ruan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruan.entity.Result;
import com.ruan.entity.User;
import com.ruan.query.UserQuery;
import com.ruan.service.UserService;
import com.ruan.utils.OssUtil;
import com.ruan.utils.RedisUtil;
import com.ruan.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName:UserController
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/6/29 10:02
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/userlogin")
    public Result login(User user, String verifyCode){
        String realVerifyCode = (String) redisUtil.get("verifyCode");
        if(!verifyCode.equalsIgnoreCase(realVerifyCode)){
            return new Result(100,"验证码不正确");
        }
        return userService.login(user);
    }

    @PostMapping("/register")
    public Result register(User user,String verifyCode){
        String realVerifyCode = (String) redisUtil.get("verifyCode");
        if(!verifyCode.equalsIgnoreCase(realVerifyCode)){
            return new Result(100,"验证码不正确");
        }
        return userService.register(user);
    }

    @RequestMapping("/getVerifyCode/{verifyCode}")
    public void setVerifyCode(@PathVariable String verifyCode){
        redisUtil.set("verifyCode",verifyCode);
    }

    @RequestMapping("/getCurrentUserId")
    public Integer getCurrentUserId(){
        Integer currentUserId = Integer.valueOf(redisUtil.get("userId").toString());
        return currentUserId;
    }

    @RequestMapping("/getRefreshToken")
    public String getRefreshToken(){
        return (String) redisUtil.get("refreshToken");
    }

    @RequestMapping("/getRefreshToken/{refreshToken}")
    public void setRefreshToken(@PathVariable String refreshToken){
        redisUtil.set("refreshToken",refreshToken);
    }

    @RequestMapping("/signOut")
    public Result signOut(){
        redisUtil.set("refreshToken",null);
        return new Result(200,"已退出登录！");
    }

    @PostMapping("/addUser")
    public Result addUser(User user){
        return userService.addUser(user);
    }

    @PostMapping("/getUserList")
    public Result getUserList(UserQuery userQuery){
        //开启分页
        PageHelper.startPage(userQuery.getPageNum(),userQuery.getPageSize());

        List<UserVo> userVoList = userService.getUserList(userQuery);

        //当前页，下一页，上一页，首页，尾页。。。。需要一个类进行封装，
        PageInfo<UserVo> userVoPageInfo = new PageInfo<>(userVoList);

        return new Result(200,userVoPageInfo);
    }

    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(MultipartFile file) throws IOException {
        //获取上传的文件名
        String fileName = file.getOriginalFilename();

        //产生新的文件名，时间戳+原先文件名后缀
        String newFileName = System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf('.'));

        String path = "useravatar/";

        String avatar = OssUtil.upload(newFileName, file.getInputStream(),path);

        return new Result(200,avatar);
    }

    @PostMapping("/editUser")
    public Result editUser(User user){
        return userService.editUser(user);
    }

    @RequestMapping("/deleteUser/{id}")
    public Result deleteUser(@PathVariable Integer id){
        return userService.deleteUser(id);
    }

    @PostMapping("/deleteUserByIds")
    public Result deleteUserByIds(@RequestParam("ids[]") Integer[] ids){
        return userService.deleteUserByIds(ids);
    }
}
